package goodsController

import (
	"CMETO/models"
	"CMETO/utils"
	"net/http"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

// 定义角色结构体
type GoodsController struct{}

// @Summary 商品查询
// @Description 查询商品
// @Tags 商品管理
// @Accept json
// @Produce json
// @Param page query string false "页码"
// @Param pageSize query string false "尺寸"
// @Param name query string false "分类名"
// @Success 200 {object} models.ResultCommon[[]models.Goods] "查询成功"
// @Success 400 {object} models.ErrorResult "查询失败"
// @Router /goods [get]
// @Security JWT
func (r GoodsController) QueryGoods(ctx *gin.Context) {
	page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
	pageSize, _ := strconv.Atoi(ctx.DefaultQuery("pageSize", "10"))
	nameStr := ctx.DefaultQuery("name", "")
	name := "%" + nameStr + "%"
	typeList := []models.GoodsFormat{}
	models.DB.Table("goods").
		Where("goods.is_delete!=1 AND goods.name like ?", name).
		Select("goods.*, goods_types.name AS sname").
		Joins("LEFT JOIN goods_types ON goods.sid = goods_types.id").
		Scan(&typeList)
	var total int64
	models.DB.Table("goods").Where("is_delete!=1 AND name like ?", name).Count(&total)
	result := &models.ResultByPage[models.GoodsFormat]{
		Msg:      "ok",
		Code:     "200",
		Total:    total,
		Page:     page,
		PageSize: pageSize,
		Result:   typeList,
	}
	ctx.JSON(http.StatusOK, result)
}

// @Summary 商品新增
// @Description 新增商品
// @Tags 商品管理
// @Accept json
// @Produce json
// @Param deptInfo body models.Goods true "商品信息"
// @Success 200 {object} models.AddResult "新增成功"
// @fail 400 {object} models.ErrorResult "数据错误"
// @fail 401 {object} models.AddResult "新增失败"
// @Router /goods [post]
// @Security JWT
func (d GoodsController) AddGoods(ctx *gin.Context) {
	goods := &models.Goods{}
	// 绑定json数据
	if err := ctx.ShouldBindJSON(&goods); err != nil {
		ctx.JSON(http.StatusOK, &models.ErrorResult{
			Msg:    "数据错误",
			Code:   "400",
			Result: nil,
		})
	} else {
		goods.CreateTime = utils.TimeFormatter(time.Now())
		var result = models.DB.Model(&models.Goods{}).Create(&goods).RowsAffected
		// 返回
		if result != 1 {
			ctx.JSON(http.StatusOK, &models.AddResult{
				Msg:    "新增失败",
				Code:   "400",
				Result: result,
			})
		} else {
			ctx.JSON(http.StatusOK, &models.AddResult{
				Msg:    "新增成功",
				Code:   "200",
				Result: result,
			})
		}
	}
}

// @Summary 商品修改
// @Description 修改商品
// @Tags 商品管理
// @Accept json
// @Produce json
// @Param deptInfo body models.Goods true "商品信息"
// @Success 200 {object} models.UpdateResult[models.Goods] "更新成功"
// @fail 400 {object} models.ErrorResult "数据错误"
// @fail 401 {object} models.AddResult "修改失败"
// @Router /goods [put]
// @Security JWT
func (d GoodsController) UpdateGoods(ctx *gin.Context) {
	var goods models.Goods
	// 绑定json数据
	if err := ctx.ShouldBindJSON(&goods); err != nil {
		ctx.JSON(http.StatusOK, &models.ErrorResult{
			Msg:    "数据绑定失败",
			Code:   "400",
			Result: err,
		})
		return
	}
	goods_old := &models.Goods{}

	models.DB.First(&goods_old, goods.Id)
	if goods_old.Id == 0 {
		ctx.JSON(http.StatusOK, &models.ErrorResult{
			Msg:    "更新失败",
			Code:   "400",
			Result: nil,
		})
		return
	}
	result := models.DB.Model(&goods_old).Save(&goods)
	if result.RowsAffected == 0 {
		ctx.JSON(http.StatusOK, &models.ErrorResult{
			Msg:    "更新失败",
			Code:   "400",
			Result: nil,
		})
	} else {
		ctx.JSON(http.StatusOK, &models.UpdateResult[models.Goods]{
			Msg:    "更新成功",
			Code:   "200",
			Result: goods,
		})
	}
}

// @Summary 商品上下架
// @Description 上下架商品
// @Tags 商品管理
// @Accept json
// @Produce json
// @Param statusInfo models.GoodsStatus int true "商品状态"
// @Success 200 {object} models.UpdateResult[models.Goods] "更新成功"
// @fail 400 {object} models.ErrorResult "数据错误"
// @fail 401 {object} models.AddResult "修改失败"
// @Router /goods/upOrDown [put]
// @Security JWT
func (d GoodsController) UpdateGoodsStatus(ctx *gin.Context) {
	var goods models.GoodsStatus
	// 绑定json数据
	if err := ctx.ShouldBindJSON(&goods); err != nil {
		ctx.JSON(http.StatusOK, &models.ErrorResult{
			Msg:    "数据绑定失败",
			Code:   "400",
			Result: err,
		})
		return
	}
	goods_old := &models.Goods{}

	models.DB.First(&goods_old, goods.Id)
	if goods_old.Id == 0 {
		ctx.JSON(http.StatusOK, &models.ErrorResult{
			Msg:    "更新失败",
			Code:   "400",
			Result: nil,
		})
		return
	}
	if goods_old.Status == 0 {
		goods_old.Status = 1
	} else {
		goods_old.Status = 0
	}
	result := models.DB.Model(&goods_old).Save(&goods_old)
	if result.RowsAffected == 0 {
		ctx.JSON(http.StatusOK, &models.ErrorResult{
			Msg:    "更新失败",
			Code:   "400",
			Result: nil,
		})
	} else {
		ctx.JSON(http.StatusOK, &models.UpdateResult[string]{
			Msg:    "更新成功",
			Code:   "200",
			Result: "",
		})
	}
}

// @Summary 删除商品
// @Description 传入json，批量删除商品
// @Tags 商品管理
// @Accept json
// @Produce json
// @Param deptsIds body models.DeleteParams true "商品id合集"
// @Success 200 {object} models.DeleteResult "更新成功"
// @fail 400 {object} models.ErrorResult "数据错误"
// @fail 401 {object} models.DeleteResult "删除失败"
// @Router /goods [delete]
// @Security JWT
func (d GoodsController) DeleteGoods(ctx *gin.Context) {
	// 绑定参数实例
	ids := &models.DeleteParams{}
	// 定义模型实例
	var goods models.Goods
	if err := ctx.ShouldBindJSON(&ids); err != nil {
		ctx.JSON(http.StatusOK, &models.ErrorResult{
			Msg:    "删除失败",
			Code:   "400",
			Result: err,
		})
	} else {
		// 假的删
		result := models.DB.Model(&goods).Where("id IN (?)", ids.Ids).Update("is_delete", 1)
		if result.RowsAffected != 0 {
			ctx.JSON(http.StatusOK, &models.DeleteResult{
				Msg:    "删除成功",
				Code:   "200",
				Result: ids.Ids,
			})
		} else {
			ctx.JSON(http.StatusOK, &models.DeleteResult{
				Msg:    "删除失败",
				Code:   "400",
				Result: []int{0},
			})
		}
	}
}
